package com.marvels.financial.loan.controller;

import com.marvels.common.dto.MarvelsApiResultDTO;
import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.exception.MarvelsApiBusinessException;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.financial.loan.dto.RepaymentNoMobileDTO;
import com.marvels.financial.loan.dto.ShouldRepaymentDTO;
import com.marvels.financial.loan.service.ReimbursementProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


/**
 * 还款流程
 * @author liuyk
 *
 */

@RequestMapping("financial/process/api")
@RestController
public class ReimbursementProcessController {
	@Autowired
	private ReimbursementProcessService reimbursementProcessService;
	/**
	 * 查看还款信息
	 * @param userId =  
	 * @return 测通
	 */
	@ResponseBody
	@RequestMapping("getRepayInfo")
	public MarvelsResultDTO getRepayInfo(@RequestBody RepaymentNoMobileDTO repaymentNoMobileDTO){
		try {
			Map map = reimbursementProcessService.getRepayInfo(repaymentNoMobileDTO);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), map);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查看还款信息FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查看还款信息异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "查看还款信息异常", null);
		} 
	}
	/**
	 * 获取还款信息（新版本）
	 * @param dto
	 * @return
	 */
	@ResponseBody
	@RequestMapping("selectPaymentInformation")
	public MarvelsResultDTO selectPaymentInformation(@RequestBody RepaymentNoMobileDTO repaymentNoMobileDTO){
		try {
			List<ShouldRepaymentDTO> list = reimbursementProcessService.selectPaymentInformation(repaymentNoMobileDTO);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), list);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询罚息调整列表FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询罚息调整列表异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "查询罚息调整列表异常", null);
		} 
	}
	
	/**
	 * 获取借据信息
	 * @param dto
	 * @return
	 */
	@ResponseBody
	@RequestMapping("queryDebt")
	public MarvelsResultDTO queryDebt(@RequestBody RepaymentNoMobileDTO repaymentNoMobileDTO){
		try {
			Map map = reimbursementProcessService.queryDebt(repaymentNoMobileDTO);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), map);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询罚息调整列表FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询罚息调整列表异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "查询罚息调整列表异常", null);
		} 
	}
	
	/**
	 * 查看应还款明细数据
	 * @param contractId = 合同号
	 * @return 测通
	 */
	@ResponseBody
	@RequestMapping("selectDetailsRepayment")
	public MarvelsResultDTO selectDetailsRepayment(@RequestBody RepaymentNoMobileDTO repaymentNoMobileDTO){
		try {
			Map map = reimbursementProcessService.selectDetailsRepayment(repaymentNoMobileDTO);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), map);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询罚息调整列表FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询罚息调整列表异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "查询罚息调整列表异常", null);
		} 
	}
	/**
	 * 还款记录
	 * @param userId = 客户ID
	 * @return 测通
	 */
	@ResponseBody
	@RequestMapping("selectRepaymentHistory")
	public MarvelsResultDTO selectRepaymentHistory(@RequestBody RepaymentNoMobileDTO repaymentNoMobileDTO){
			List<Map<String,Object>> listMap =  reimbursementProcessService.selectRepaymentHistory(repaymentNoMobileDTO);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), listMap);
	}
	/**
	 * 查出扣款明细
	 * @param orgTransctionSeq = 还款冲帐序列号
	 * @return  测试通过
	 */
	@ResponseBody
	@RequestMapping("penaltyAdjustmentList")
	public MarvelsResultDTO penaltyAdjustmentList(@RequestBody RepaymentNoMobileDTO repaymentNoMobileDTO){
			List listMap = reimbursementProcessService.penaltyAdjustmentList(repaymentNoMobileDTO);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), listMap);
	}
	
	
	/**
	 * 提交还款
	 * @params    loanId,userBankId,contractId,amount,payType,
	 * @return
	 */
	@ResponseBody
	@RequestMapping("repaymentNoMobile")
	public MarvelsApiResultDTO repaymentNoMobile(@RequestBody RepaymentNoMobileDTO repaymentNoMobileDTO){
		try {
			MarvelsApiResultDTO result = reimbursementProcessService.repaymentNoMobile(repaymentNoMobileDTO);
			return result;
		}catch (MarvelsApiBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>提交还款FAIL:", e);
			return   MarvelsApiResultDTO.getResult(e.getCode(),e.getMessage(),null,null,null);// MarvelsApiResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>提交还款", e);
			return  MarvelsApiResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "提交还款异常", null,null,null);
		} 
	}
	
	
	
	/**
	 * 查看应还款计划
	 * @param contractId = 合同号
	 * @return 测通
	 */
	@ResponseBody
	@RequestMapping("repayPlan")
	public MarvelsResultDTO repayPlan(@RequestBody RepaymentNoMobileDTO repaymentNoMobileDTO){
			List<Map> map = reimbursementProcessService.repayPlan(repaymentNoMobileDTO);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), map);
	}
	/**
	 * 永久更换还款卡
	 * @param repaymentNoMobileDTO
	 * @return
	 */
	@ResponseBody
	@RequestMapping("updateBank")
	public MarvelsResultDTO updateBank(@RequestBody RepaymentNoMobileDTO repaymentNoMobileDTO){
		try {
			int count = reimbursementProcessService.updateBank(repaymentNoMobileDTO);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), count);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询罚息调整列表FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询罚息调整列表异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "查询罚息调整列表异常", null);
		} 
	}
	
}
